class Sms
  attr_accessor :mobile, :code

  SIGN_NAME = '上海先虑'
  TEMPLATE_CODE = 'SMS_100890077'
  APP_CODE = '5e7673c29137450e9f4c52faae889f76'

  def initialize mobile=nil, code=nil
    self.mobile = mobile
    self.code = code
  end

  def send_code
    self.mobile = [self.mobile] unless self.mobile.is_a?(Array)
    result = RestClient::Request.execute(method: :get,
                                         url: "http://sms.market.alicloudapi.com/singleSendSms",
                                         headers: {"Authorization" => "APPCODE #{APP_CODE}"},
                                         payload: {
                                             "ParamString" => "{'code':'#{self.code}'}",
                                             "RecNum" => "#{self.mobile.join(',')}",
                                             "SignName" => "#{SIGN_NAME}",
                                             "TemplateCode" => "#{TEMPLATE_CODE}",
                                             "Content-Type" => "application/json"
                                         })
    JSON.parse(result)
  end

  def self.send_code mobile, code
    new(mobile, code).send_code
  end
end